{% extends "base.html" %}

{% block content %}
<div class="row justify-content-center">
    <div class="col-md-10">
        <!-- 文章内容 -->
        <div class="card mb-4">
            <div class="card-body">
                <!-- 文章标题 -->
                <h1 class="card-title mb-4">{{ post.title }}</h1>

                <!-- 文章元信息 -->
                <div class="d-flex justify-content-between align-items-center mb-4 text-muted">
                    <div>
                        <span>
                            <a href="{{ url_for('user_profile', username=post.author.username) }}"
                               class="text-decoration-none text-muted">
                                作者: {{ post.author.username }}
                            </a>
                        </span>
                        <span class="ms-3">发布时间: {{ post.date_posted.strftime('%Y-%m-%d %H:%M') }}</span>
                    </div>

                    <!-- 操作按钮 -->
                    {% if current_user.is_authenticated and current_user == post.author %}
                    <div class="btn-group">
                        <a href="{{ url_for('update_post', post_id=post.id) }}" class="btn btn-outline-primary btn-sm">编辑</a>
                        <a href="{{ url_for('delete_post', post_id=post.id) }}" class="btn btn-outline-danger btn-sm"
                           onclick="return confirm('确定要删除这篇文章吗？')">删除</a>
                    </div>
                    {% endif %}
                </div>

                <!-- 文章内容 -->
                <div class="post-content">
                    {{ post.content|replace('\n', '<br>')|safe }}
                </div>
            </div>
        </div>

        <!-- 评论区域 -->
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">
                    评论
                    <span class="badge bg-primary">{{ post.comments|length }}</span>
                </h5>
            </div>

            <div class="card-body">
                <!-- 评论表单 -->
                {% if current_user.is_authenticated %}
                <div class="mb-4">
                    <form method="POST" action="{{ url_for('new_comment', post_id=post.id) }}">
                        <div class="mb-3">
                            <label for="content" class="form-label">发表评论</label>
                            <textarea class="form-control" id="content" name="content" rows="3"
                                      placeholder="写下你的评论..." maxlength="500" required></textarea>
                            <div class="form-text">最多500字</div>
                        </div>
                        <button type="submit" class="btn btn-primary">发布评论</button>
                    </form>
                </div>
                {% else %}
                <div class="alert alert-info">
                    <a href="{{ url_for('login') }}" class="alert-link">登录</a>后即可发表评论
                </div>
                {% endif %}

                <!-- 评论列表 -->
                <div class="comments-section">
                    <h6>全部评论</h6>

                    {% if post.comments %}
                        {% for comment in post.comments %}
                        <div class="card mb-3">
                            <div class="card-body">
                                <div class="d-flex justify-content-between align-items-start">
                                    <div class="flex-grow-1">
                                        <div class="d-flex align-items-center mb-2">
                                            <strong>
                                                <a href="{{ url_for('user_profile', username=comment.author.username) }}"
                                                   class="text-decoration-none">
                                                    {{ comment.author.username }}
                                                </a>
                                            </strong>
                                            <small class="text-muted ms-2">
                                                {{ comment.date_posted.strftime('%Y-%m-%d %H:%M') }}
                                            </small>
                                        </div>
                                        <p class="card-text mb-0">{{ comment.content }}</p>
                                    </div>

                                    <!-- 删除评论按钮 -->
                                    {% if current_user.is_authenticated and
                                          (current_user == comment.author or current_user == post.author) %}
                                    <div class="ms-3">
                                        <a href="{{ url_for('delete_comment', comment_id=comment.id) }}"
                                           class="btn btn-outline-danger btn-sm"
                                           onclick="return confirm('确定要删除这条评论吗？')">
                                            删除
                                        </a>
                                    </div>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                        {% endfor %}
                    {% else %}
                        <div class="text-center py-4">
                            <p class="text-muted">还没有评论，快来发表第一条评论吧！</p>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>

        <!-- 返回按钮 -->
        <div class="mt-3">
            <a href="{{ url_for('index') }}" class="btn btn-secondary">返回首页</a>
        </div>
    </div>
</div>
{% endblock %}